package com.cn.xiaonuo.slbfinances.slbagents.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.xiaonuo.common.entity.BusinessMessage;
import com.cn.xiaonuo.common.utils.StringUtils;
import com.cn.xiaonuo.dbs.core.annotation.DataSource;
import com.cn.xiaonuo.slbfinances.members.entity.SlUser;
import com.cn.xiaonuo.slbfinances.members.mapper.SlUserMapper;
import com.cn.xiaonuo.slbfinances.slbagents.entity.GzUser;
import com.cn.xiaonuo.slbfinances.slbagents.entity.HpcUser;
import com.cn.xiaonuo.slbfinances.slbagents.mapper.GzUsersMapper;
import com.cn.xiaonuo.slbfinances.slbagents.mapper.HpcUsersMapper;
import com.cn.xiaonuo.slbfinances.slbagents.service.ICoverageAgentShopService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class CoverageAgentShopServiceImpl extends ServiceImpl<SlUserMapper,SlUser> implements ICoverageAgentShopService{
    @Resource
    private SlUserMapper slUserMapper;
    @Resource
    private HpcUsersMapper hpcUsersMapper;
    @Resource
    private GzUsersMapper gzUsersMapper;
    //查询用户搜了宝国甄和平村账号信息
    @DataSource(name = "jyb_soole_data_hn")
    @Override
    public BusinessMessage queryAccountInfo(String slbId, String phone){
        BusinessMessage<Object> message = new BusinessMessage<>();
        String hpcId = "";
        String hpcRealName = "";
        String hpcAccounts = "";
        String gzId = "";
        String gzRealName = "";
        String gzAccounts = "";
        try {
            if(StringUtils.isNotBlank(slbId) && StringUtils.isNotBlank(phone)){
                message.setMessage("电话和搜了宝ID只能输入一个");
                message.setSuccess(false);
                return message;
            }
            //查询用户搜了宝信息
            if(StringUtils.isNotBlank(slbId)){
                //根据id查询出电话号码
                SlUser slUser = slUserMapper.selectById(slbId);
                phone = slUser.getPhone();
            }else if(StringUtils.isNotBlank(phone)){
                //根据电话号码查询出搜了宝id
                String finalPhone = phone;
                QueryWrapper queryWrapper = new QueryWrapper();
                queryWrapper.setEntity(new SlUser(){{
                    setPhone(finalPhone);
                }});
                SlUser slUser = slUserMapper.selectOne(queryWrapper);
                slbId = slUser.getId().toString();
                //获得和平村信息
                HpcUser hpcUser = this.getHpcUser(phone);
                hpcId = hpcUser.getId().toString();
                hpcRealName = hpcUser.getRealName();
                hpcAccounts =hpcUser.getAccounts();
                //获得国甄信息
                GzUser gzUser = this.getGzUser(phone);
                gzId = gzUser.getId().toString();
                gzRealName = gzUser.getRealName();
                gzAccounts = gzUser.getAccounts();

            }else{
                message.setMessage("请输入电话或搜了宝ID");
                message.setSuccess(false);
                return message;
            }
            //封装参数
            Map<String, Object> data = new HashMap<>();
            data.put("phone",phone);
            data.put("slbId",slbId);
            data.put("hpcId",hpcId);
            data.put("hpcRealName",hpcRealName);
            data.put("hpcAccounts",hpcAccounts);

            data.put("gzId",gzId);
            data.put("gzRealName",gzRealName);
            data.put("gzAccounts",gzAccounts);
            //3.返回
            message.setData(data);
            message.setMessage("操作成功");
            message.setSuccess(true);
            return message;
        } catch (Exception e) {
            e.printStackTrace();
            message.setMessage("操作失败");
            message.setSuccess(false);
            return message;
        }
    }
    //从和平村的库查询和平村用户信息
    @Override
    public HpcUser getHpcUser(String phone){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(new HpcUser(){{
            setPhone(phone);
        }});
        return hpcUsersMapper.selectOne(queryWrapper);
    }
    //从和平村的库查询和平村用户信息
    @DataSource(name = "gz_new")
    @Override
    public GzUser getGzUser(String phone){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(new GzUser(){{
            setPhone(phone);
        }});
        return gzUsersMapper.selectOne(queryWrapper);
    }
}
